// ActionScript file
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.events.ValidationResultEvent;
import mx.managers.PopUpManager;  
import mx.containers.TitleWindow; 

import general.dataObjects.ItemDO;
import inbound.receiving.dataObjects.ReceiptDetailDO;
import general.components.ItmQF;
import common.utils.StringManager;
import common.utils.PopupWindowUtil;

[Bindable]
[Embed(source="assets/images/search_16x16.png")]
private var search_16x16:Class;
[Bindable]private var _selectedReceiptDtl:ReceiptDetailDO = new ReceiptDetailDO();	
[Bindable]private var iBaseQty:int;
[Bindable]private var iOpenQty:int;
[Bindable]private var stBaseUM:String;			
private var _acQtyUM:ArrayCollection = new ArrayCollection();
//private var _acLocRule:ArrayCollection = new ArrayCollection();
//private var _acInvSts:ArrayCollection = new ArrayCollection();	
private var _acVal:Array = new Array(); 
public var mainApp:Object = null;
public var callbackFunction:Function;


private function initData():void{
	var wsdl:String = this.parentApplication.AppServer + "ReceivingService.asmx?wsdl";
	ReceivingSvs.loadWSDL(wsdl);
	wsdl = this.parentApplication.AppServer + "InventoryService.asmx?wsdl";
	InventorySvs.loadWSDL(wsdl);
	//wsdl = this.parentApplication.AppServer + "GeneralInforService.asmx?wsdl";
	//GeneralInfoSvs.loadWSDL(wsdl);
	
	//GeneralInfoSvs.GetLocatingRules(this.parentApplication.SessionID);
	//GeneralInfoSvs.GetInventorySts(this.parentApplication.SessionID);
	
	//if (this._action == "SAVE")
	if (Number(this._selectedReceiptDtl.INTERNAL_RECEIPT_LINE_NUM) > 0){
		InventorySvs.GetItemUMsASC(this.parentApplication.SessionID, 
			this._selectedReceiptDtl.COMPANY, 
			this._selectedReceiptDtl.ITEM);
	}
	_acVal.push(VAL_ITEM);
	_acVal.push(VAL_TOTAL_QTY);
	
	setDisplayProperties();
}

public function set SelectedReceiptDetail(value:ReceiptDetailDO):void{
	this._selectedReceiptDtl = value;
}

//closeWindow    
private function closeWindow():void {    
	PopUpManager.removePopUp(this);     
	if (callbackFunction != null) {    
		//调用父窗体的方法并以参数形式返回选择结果    
		callbackFunction.call(mainApp);     
	}   
}

private function setDisplayProperties():void{
	if (this._selectedReceiptDtl.COMPANY != null){
		this.COMPANY.enabled = false;
	}
}
/*
private function bindInventorySts(event:ResultEvent):void{
	var xmlObj:XML = new XML(event.result.toString());
	
	for each(var xmlNode:XML in xmlObj.children()){
		_acInvSts.addItem({label:xmlNode.DESCRIPTION.toString(), data:xmlNode.IDENTIFIER.toString()});
	}
	this.INVENTORY_STS.dataProvider = _acInvSts;
}
*/
private function bindQtyUM(event:ResultEvent):void{
	var xmlObj:XML = new XML(event.result.toString());
	_acQtyUM.removeAll();				
	for each(var xmlNode:XML in xmlObj.children()){
		_acQtyUM.addItem({label:xmlNode.DESCRIPTION.toString(), data:xmlNode.QUANTITY_UM.toString(), value:xmlNode.CONVERSION_QTY});
	}
	this.QUANTITY_UM.dataProvider = _acQtyUM;	
	if (_acQtyUM != null && _acQtyUM.length > 0)			
		this.stBaseUM = _acQtyUM[0].data;
	//this.QUANTITY_UM.selectedData = this._rcpDtlDO.QUANTITY_UM;
}
/*
private function bindLocRule(event:ResultEvent):void{
	var xmlObj:XML = new XML(event.result.toString());
	
	for each(var xmlNode:XML in xmlObj.children()){
		_acLocRule.addItem({label:xmlNode.LOCATING_RULE.toString(), data:xmlNode.LOCATING_RULE.toString()});
	}
	this.LOCATING_RULE.dataProvider = _acLocRule;	
}
*/
private function onFault(event:FaultEvent):void{
	Alert.show(event.message.toString(), "错误");
}

private function validateForm():Boolean {
	var arError:Array = Validator.validateAll(this._acVal);;
	var isValid:Boolean = (arError.length == 0);
	if (isValid)
		return true;
	else {
		var event:ValidationResultEvent;
		var arErrMsg:Array = [];
		for each (event in arError) {
			//var errField:String = FormItem(event.currentTarget.source.parent).label;
			arErrMsg.push(event.message);
		}
		Alert.show(arErrMsg.join("\n"), "错误", Alert.OK);
		return false;
	}
} 

private function btnSave_Click():void{			
	if (validateForm()){
		if (Number(this.TOTAL_QTY.text) == 0){
			Alert.show("错误：数量需大于0", "错误");
			return;
		}
		//this.selectedReceiptDtl.RECEIPT_ID = this.RECEIPT_ID.text;
		var iConvQty:int = this.QUANTITY_UM.selectedItem.value;
		var iQty:int = Number(this.TOTAL_QTY.text);
		this.iBaseQty = iQty * iConvQty;
		iQty = Number(this.OPEN_QTY.text);
		this.iOpenQty = iQty * iConvQty;
		var stXML:String = this.model.toString();
		ReceivingSvs.SaveReceiptDtl(this.parentApplication.SessionID, stXML);
	}
}

private function btnCheckIn_Click():void{
	//
}
private function btnDelete_Click():void{
	//
}
private function btnNew_Click():void{
	var tmpRcpDtlDO:ReceiptDetailDO = this._selectedReceiptDtl;
	this._selectedReceiptDtl = new ReceiptDetailDO();
	this._selectedReceiptDtl.WAREHOUSE = tmpRcpDtlDO.WAREHOUSE;
	this._selectedReceiptDtl.RECEIPT_ID = tmpRcpDtlDO.RECEIPT_ID;
	this._selectedReceiptDtl.COMPANY = tmpRcpDtlDO.COMPANY;
	this._selectedReceiptDtl.INTERNAL_RECEIPT_NUM = tmpRcpDtlDO.INTERNAL_RECEIPT_NUM;
	tmpRcpDtlDO = null;
}

private function onSaveResult(event:ResultEvent):void{
	if (null != event.result){				
		var errMsg:String;
		errMsg = event.result as String
		
		if (StringManager.IsErrorMsg(errMsg))
			Alert.show(errMsg);
		else{
			Alert.show("保存成功");
			
			var tmpRcpDtl:ReceiptDetailDO = this._selectedReceiptDtl;
			this._selectedReceiptDtl = new ReceiptDetailDO();
			this._selectedReceiptDtl.INTERNAL_RECEIPT_LINE_NUM = 0;
			this._selectedReceiptDtl.INTERNAL_RECEIPT_NUM = tmpRcpDtl.INTERNAL_RECEIPT_NUM;
			this._selectedReceiptDtl.RECEIPT_ID = tmpRcpDtl.RECEIPT_ID;
			this._selectedReceiptDtl.COMPANY = tmpRcpDtl.COMPANY;
			this._selectedReceiptDtl.WAREHOUSE = tmpRcpDtl.WAREHOUSE;
			tmpRcpDtl = null;
			
		}
	}
	
}

private function btnItemLookUp_Click():void{
	var lookupDlg:ItmQF = new ItmQF();
	
	lookupDlg.item = ITEM.text;
	lookupDlg.company = COMPANY.selectedItem.data;
	lookupDlg.width = 800;    
	lookupDlg.height = 600;      
	lookupDlg.mainApp = this;
	lookupDlg.title = "货品查找"; 
	lookupDlg.callbackFunction = this.setChoosedItem;   
	
	PopupWindowUtil.popupWindow(this.parentApplication, this, lookupDlg); 
}

private function setChoosedItem(itm:ItemDO):void{
	this.ITEM.text = itm.ITEM;
	this.ITEM_DESC.text = itm.ITEM_DESC;
	this.ITEM_CLASS.text = itm.ITEM_CLASS;
	this.COMPANY.selectedData = itm.COMPANY;
	this.LOCATING_RULE.selectedData = itm.LOCATING_RULE;
	if (itm.ATTRIBUTE_TRACK == "Y"){
		this.ATTRIBUTE_TRACK.selected = true;
		this.ATTRIBUTE1.editable = true;
		this.ATTRIBUTE2.editable = true;
		this.ATTRIBUTE3.editable = true;
		this.ATTRIBUTE4.editable = true;
		this.ATTRIBUTE5.editable = true;
		this.ATTRIBUTE6.editable = true;
		this.ATTRIBUTE7.editable = true;
		this.ATTRIBUTE8.editable = true;
	}				
	focusManager.setFocus(this.TOTAL_QTY);
	InventorySvs.GetItemUMsASC(this.parentApplication.SessionID, itm.COMPANY, itm.ITEM);				
}

private function updateOpenQty():void{
	var newTotalQty:Number = Number(this.TOTAL_QTY.text);
	if (null != this.OPEN_QTY)
		this.OPEN_QTY.text = String(newTotalQty - this._selectedReceiptDtl.TOTAL_QTY + this._selectedReceiptDtl.OPEN_QTY);
}